package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysDeptAddDTO;
import com.framework.system.entity.dto.SysDeptEditDTO;
import com.framework.system.entity.dto.SysDeptQueryDTO;
import com.framework.system.entity.vo.SysDeptListVO;
import com.framework.system.entity.vo.SysDeptVO;
import com.framework.system.service.ISysDeptService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysDept")
@AllArgsConstructor
public class SysDeptController {

    private ISysDeptService sysDeptService;

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    @SaCheckPermission("system:sysDept:selectPage")
    @AutoLog(module = "部门管理", title = "分页查询")
    public R<Page<SysDeptListVO>> selectPage(Page reqPage, SysDeptQueryDTO req) {
        return R.success(sysDeptService.selectPage(reqPage, req));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @SaCheckPermission("system:sysDept:add")
    @AutoLog(module = "部门管理", title = "新增")
    public R<Void> add(@Validated @RequestBody SysDeptAddDTO req) {
        sysDeptService.add(req);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @SaCheckPermission("system:sysDept:edit")
    @AutoLog(module = "部门管理", title = "修改")
    public R<Void> edit(@Validated @RequestBody SysDeptEditDTO req) {
        sysDeptService.edit(req);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @SaCheckPermission("system:sysDept:remove")
    @AutoLog(module = "部门管理", title = "删除")
    public R<Void> remove(@RequestParam String ids) {
        sysDeptService.remove(ids);
        return R.success();
    }

    /**
     * 查看
     */
    @GetMapping("/view")
    @SaCheckPermission("system:sysDept:view")
    @AutoLog(module = "部门管理", title = "查看")
    public R<SysDeptVO> view(@RequestParam String id) {
        return R.success(sysDeptService.view(id));
    }

    /**
     * 查询部门树
     */
    @GetMapping("/listDeptTree")
    @AutoLog(module = "部门管理", title = "查询部门树")
    public R<List<Tree<String>>> listDeptTree() {
        return R.success(sysDeptService.listDeptTree());
    }

    /**
     * 查询下拉部门树，排除当前节点以及子节点
     */
    @GetMapping("/selectDeptTreeAndExcludeNode")
    @AutoLog(module = "部门管理", title = "查询下拉部门树，排除当前节点以及子节点")
    public R<List<Tree<String>>> selectTreeAndExcludeNode(@RequestParam(required = false) String nodeId) {
        return R.success(sysDeptService.selectDeptTreeAndExcludeNode(nodeId));
    }

    /**
     * 查询下拉部门树
     */
    @GetMapping("/selectDeptTree")
    @AutoLog(module = "部门管理", title = "查询下拉部门树")
    public R<List<Tree<String>>> selectTree() {
        return R.success(sysDeptService.selectDeptTree());
    }

}

